<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2011 PrestaShop SA
*  @version  Release: $Revision: 10947 $
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

class Customer extends CustomerCore
{
	/** @var string Username */
	public 	$username;
	public 	$name;
	public  $address;
	public  $parent;
	public 	$phone;
	public 	$city;
	public  $code;

	protected 	$fieldsValidate = array('username' => 'isGenericName', 'name' => 'isGenericName', 'email' => 'isEmail', 'passwd' => 'isPasswd',
		 'birthday' => 'isBirthDate');

	protected 	$fieldsRequired = array();
 	protected 	$fieldsSize = array();

	public function getFields()
	{
//		parent::validateFields();
		if (isset($this->birthday))
			$fields['id_customer'] = (int)($this->id);
//		if (isset($this->birthday))	
		$fields['birthday'] = pSQL($this->birthday);
		$fields['email'] = pSQL($this->email);
		$fields['passwd'] = pSQL($this->passwd);
		$fields['last_passwd_gen'] = pSQL($this->last_passwd_gen);
		$fields['active'] = (int)($this->active);
		$fields['date_add'] = pSQL($this->date_add);
		$fields['date_upd'] = pSQL($this->date_upd);
		//add username to db
		$fields['username'] = pSQL($this->username);
		$fields['name'] = pSQL($this->name);
		$fields['address'] = pSQL($this->address);
		$fields['parent'] = (int)($this->parent);
		$fields['phone'] = pSQL($this->phone);
		$fields['city'] = pSQL($this->city);
		$fields['code'] = pSQL($this->code);
		return $fields;
	}
	
	/**
	  * Return customer instance from its username (optionnaly check password)
	  *
	  * @param string $username username account
	  * @param string $passwd Password is also checked if specified
	  * @return Customer instance
	  */
	public function getByUsername($username, $passwd = NULL)
	{
	 	if ($passwd AND !Validate::isPasswd($passwd))
	 		die (Tools::displayError());

		$result = Db::getInstance()->getRow('
		SELECT *
		FROM `'._DB_PREFIX_	.'customer`
		WHERE `active` = 1
		AND `username` = \''.pSQL($username).'\'
		'.(isset($passwd) ? 'AND `passwd` = \''.md5(pSQL(_COOKIE_KEY_.$passwd)).'\'' : '').'
		AND `deleted` = 0
		AND `is_guest` = 0');

		if (!$result)
			return false;
		$this->id = $result['id_customer'];
		foreach ($result AS $key => $value)
			if (key_exists($key, $this))
				$this->{$key} = $value;

		return $this;
	}
	
	/**
	  * Check if username is already registered in database
	  *
	  * @param string $username username
	  * @param $return_id boolean
	  * @param $ignoreGuest boolean, to exclude guest customer
	  * @return Customer ID if found, false otherwise
	  */
	public static function usernameExists($username, $return_id = false, $ignoreGuest = true)
	{
		$result = Db::getInstance()->getRow('
		SELECT `id_customer`
		FROM `'._DB_PREFIX_.'customer`
		WHERE `username` = \''.pSQL($username).'\''
		.($ignoreGuest ? 'AND `is_guest` = 0' : ''));

		if ($return_id)
			return (int)($result['id_customer']);
		else
			return isset($result['id_customer']);
	}
}

